package com.lean.android.ad.utils

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.provider.Settings
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager

/**
 * copy from QMUIDisplayHelper.
 */
object DisplayHelper {

    private val DENSITY = Resources.getSystem().displayMetrics.density

    fun removeFromParent(view: View) {
        val parent = view.parent
        if (parent is ViewGroup) {
            parent.removeView(view)
        }
    }

    fun getDisplayMetrics(context: Context): DisplayMetrics {
        return context.resources.displayMetrics
    }

    fun dpToPx(dp: Int): Int = (dp * DENSITY + 0.5f).toInt()

    fun pxToDp(px: Int): Int = (px / DENSITY + 0.5f).toInt()

    /**
     * 单位转换: dp -> px
     *
     * @param dp
     * @return
     */
    @JvmStatic
    fun dp2px(context: Context, dp: Int): Int {
        return (getDensity(context) * dp + 0.5).toInt()
    }

    fun getDensity(context: Context): Float {
        return context.resources.displayMetrics.density
    }

    fun getScreenWidth(context: Context): Int = getDisplayMetrics(context).widthPixels

    fun getScreenHeight(context: Context): Int {
        var screenHeight: Int = getDisplayMetrics(context).heightPixels
        if (com.lean.android.ad.utils.DeviceHelper.isXiaomi() && xiaomiNavigationGestureEnabled(context)) {
            screenHeight += getResourceNavHeight(context)
        }
        return screenHeight
    }

    private fun getResourceNavHeight(context: Context): Int {
        // 小米4没有nav bar, 而 navigation_bar_height 有值
        val resourceId =
            context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            context.resources.getDimensionPixelSize(resourceId)
        } else -1
    }

    @JvmStatic
    fun getStatusBarHeight(context: Context): Int {
        if (com.lean.android.ad.utils.DeviceHelper.isXiaomi()) {
            val resourceId =
                context.resources.getIdentifier("status_bar_height", "dimen", "android")
            return if (resourceId > 0) {
                context.resources.getDimensionPixelSize(resourceId)
            } else 0
        }
        try {
            val c = Class.forName("com.android.internal.R\$dimen")
            val obj = c.getDeclaredConstructor().newInstance()
            val field = c.getField("status_bar_height")
            val x = field?.get(obj).toString().toInt()
            if (x > 0) {
                return context.resources.getDimensionPixelSize(x)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }


    // ====================== Setting ===========================
    private const val VIVO_NAVIGATION_GESTURE = "navigation_gesture_on"
    private const val HUAWAI_DISPLAY_NOTCH_STATUS = "display_notch_status"
    private const val XIAOMI_DISPLAY_NOTCH_STATUS = "force_black"
    private const val XIAOMI_FULLSCREEN_GESTURE = "force_fsg_nav_bar"

    fun vivoNavigationGestureEnabled(context: Context): Boolean {
        val result = Settings.Secure.getInt(
            context.contentResolver, VIVO_NAVIGATION_GESTURE, 0
        )
        return result != 0
    }


    fun xiaomiNavigationGestureEnabled(context: Context): Boolean {
        val result = Settings.Global.getInt(
            context.contentResolver, XIAOMI_FULLSCREEN_GESTURE, 0
        )
        return result != 0
    }


    @JvmStatic
    fun huaweiIsNotchSetToShowInSetting(context: Context): Boolean {
        // 0: 默认
        // 1: 隐藏显示区域
        val result = Settings.Secure.getInt(
            context.contentResolver, HUAWAI_DISPLAY_NOTCH_STATUS, 0
        )
        return result == 0
    }

    fun xiaomiIsNotchSetToShowInSetting(context: Context): Boolean {
        return Settings.Global.getInt(
            context.contentResolver, XIAOMI_DISPLAY_NOTCH_STATUS, 0
        ) == 0
    }

    fun setFullScreen(activity: Activity) {
        val window = activity.window
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

    /**
     * 取消全屏
     *
     * @param activity
     */
    fun cancelFullScreen(activity: Activity) {
        val window = activity.window
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
    }

    /**
     * 判断是否全屏
     *
     * @param activity
     * @return
     */
    fun isFullScreen(activity: Activity): Boolean {
        val params = activity.window.attributes
        return params.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN == WindowManager.LayoutParams.FLAG_FULLSCREEN
    }

}